﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using wenku8.Entity;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace wenku8.Xamls
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class ChapterIndexPage : Page
    {
        internal List<object> linkageAndTitles;
        internal List<object> parameters;

        private static string localTitle;

        public ChapterIndexPage()
        {
            this.InitializeComponent();
            parameters = new List<object>();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.New) {
                linkageAndTitles = e.Parameter as List<object>;
                localTitle = MainPage.TitleTextBlock.Text = (linkageAndTitles[0] as BookIndex).VolumnTitle;
                chapterIndexListView.ItemsSource = (linkageAndTitles[0] as BookIndex).ChapterTitles;
                MainPage.DisableProgressRing();
            }
            else if (e.NavigationMode == NavigationMode.Back) {
                MainPage.TitleTextBlock.Text = localTitle;
                MainPage.DisableProgressRing();
            }
        }

        private void ChapterIndexListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var titleAndLinkage = (TitleAndLinkage)e.ClickedItem;
            if (titleAndLinkage != null) {
                MainPage.TitleTextBlock.Text = titleAndLinkage.ChapterTitle;
                var novelContentUri = (linkageAndTitles[1] as string).Replace("index.htm", titleAndLinkage.InterLinkage);
                MainPage.EnableProgressRing();
                this.Frame.Navigate(typeof(NovelContentPage), novelContentUri);
            }
        }
    }
}
